/*
 * Dictionary object wrapper around an associative array.
 */

// Get the keys of the dictionary as an array.
function ArrayDictionary_keys() {
  var keys = new Array();
  for (var i in this.dictionary) {
    keys.push(i);
  }
  return keys;
}

// Get the items of the dictionary as an array.
function ArrayDictionary_items() {
  var items = new Array();
  for (var i in this.dictionary) {
    items.push(this.dictionary[i]);
  }
  return items;
}

// Get an array of key, value pairs from a dictionary.
function ArrayDictionary_keyItems() {
  return this.dictionary;
}

// Wrapper around Script.Dictionary.Exists.
function ArrayDictionary_exists(key) {
  return key in this.dictionary;
}

// Wrapper around Script.Dictionary.Item property.
// To assign a key a value use the add() function.
function ArrayDictionary_item(key) {
  return this.dictionary[key];
}

// Wrapper around Script.Dictionary.Key property
// To assign a key a value use the add() function.
function ArrayDictionary_key(key) {
  return key;
}

// Add / replace a key value pair.
function ArrayDictionary_add(key, item) {
  if (!this.exists(key)) {
    this.length += 1;
  }
  this.dictionary[key] = item;
}

// Wrapper around Script.Dictionary.Remove method.
function ArrayDictionary_remove(key) {
  if (this.exists(key)) {
    var new_array = new Array();
    for (var i in this.dictionary) {
      if (i != key) {
        new_array[i] = this.dictionary[i];
      }
    }
    this.dictionary = new_array;
    this.length -= 1;
  }
}

// Wrapper around Script.Dictionary.RemoveAll
function ArrayDictionary_removeAll() {
  this.length = 0;
  this.dictionary = {};
}

// Constructor.
function ArrayDictionary(associative_array) {
  // The underlying dictionary object.
  this.dictionary = defaultValue(associative_array, {});
  // Mirror the length property of the array.
  this.length = 0;
}

// Fix up the class methods.
inherit(ArrayDictionary, Dictionary);
ArrayDictionary.prototype.add = ArrayDictionary_add;
ArrayDictionary.prototype.keys = ArrayDictionary_keys;
ArrayDictionary.prototype.items = ArrayDictionary_items;
ArrayDictionary.prototype.keyItems = ArrayDictionary_keyItems;
ArrayDictionary.prototype.exists = ArrayDictionary_exists;
ArrayDictionary.prototype.item = ArrayDictionary_item;
ArrayDictionary.prototype.key = ArrayDictionary_key;
ArrayDictionary.prototype.remove = ArrayDictionary_remove;
ArrayDictionary.prototype.removeAll = ArrayDictionary_removeAll;
